package com.zimmur.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.zimmur.common.AppModuleEnum;
import com.zimmur.model.account.GoodsUser;
import com.zimmur.model.account.UserAddress;
import com.zimmur.model.account.UserSizeInfo;
import com.zimmur.model.base.AdBanner;
import com.zimmur.model.goods.Goods;
import com.zimmur.model.goods.GoodsColor;
import com.zimmur.model.goods.GoodsImage;
import com.zimmur.model.goods.GoodsSize;
import com.zimmur.model.goods.GoodsType;
import com.zimmur.model.goods.HomeMadeExample;
import com.zimmur.model.zone.Topic;
import com.zimmur.model.zone.view.IndexView;
import com.zimmur.service.IHomeMadeService;
import com.zimmur.service.IUserService;
import com.zimmur.service.account.IUserAddressService;
import com.zimmur.service.account.IUserSizeInfoService;
import com.zimmur.service.base.IAdBannerService;
import com.zimmur.service.goods.IGoodsImageService;
import com.zimmur.service.goods.IGoodsRecommandService;
import com.zimmur.service.goods.IGoodsService;
import com.zimmur.service.goods.IGoodsSizeService;
import com.zimmur.service.goods.IGoodsTypeService;
import com.zimmur.service.zone.ITopicService;
import com.zimmur.tools.PageUtil;

@Service("homeMadeServiceImpl")  
public class HomeMadeServiceImpl implements IHomeMadeService{
	private static Logger log=LoggerFactory.getLogger(HomeMadeServiceImpl.class);
    @Resource  
    private IGoodsService goodsService;
    @Resource  
    private IGoodsTypeService goodsTypeService;
    @Resource  
    private IGoodsImageService goodsImageService;    
    @Resource  
    private IGoodsSizeService goodsSizeService;
    @Resource  
    private IGoodsRecommandService goodsRecommandService;

    @Resource  
    private IUserService userService;
    @Resource  
    private IUserAddressService userAddressService;
    @Resource  
    private IUserSizeInfoService userSizeInfoService;
	@Resource
	private IAdBannerService abBannerService;
	@Resource
	private ITopicService topicService;

	@Override
	public IndexView homeMadeIndex(long userId,Integer pageNow) {
		IndexView indexView = new IndexView();
//		PageUtil<Goods>page = new PageUtil<>();
		try {
			//轮播广告 10=公用模块(首页)/40=衣匠模块/60=家定模块
			List<AdBanner> adBannerList = abBannerService.selectAdList(Integer.parseInt(AppModuleEnum.DesigerMake.getModuleId()));
			if(!adBannerList.isEmpty() && adBannerList.size() > 0){
				indexView.setAdBannerList(adBannerList);
			}
			//商品类型 信息 
			Short typeFlag=2;//类别：1=衣匠/2=家定
			List<GoodsType> goodsTypeList =goodsTypeService.selectByTypeFlagGoodsType(typeFlag);
			if(!goodsTypeList.isEmpty() && goodsTypeList.size() > 0){
				indexView.setGoodsTypeList(goodsTypeList);
			}
			if(pageNow == 0){
				pageNow=1;
			}
			
			//获取推荐话题
			PageUtil<Topic> recommandTopicList = homeMadeIndexPage(userId, pageNow);
			List<Topic> topicListLists = recommandTopicList.getItemList();
			if(!topicListLists.isEmpty() && topicListLists.size() > 0){
	    		indexView.setTopicList(topicListLists);
	    	}else{
	    		indexView.setTopicList(new ArrayList<Topic>());
	    	}
			
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		
		return indexView;
	}

	@Override
	public Goods madeDetail(long goodsId, long userId) {
		Goods goods= null;
		try {
//			goods = goodsService.selectByGoodsId(goodsId);
			goods = goodsService.selectByGoodsIdAndIsCollect(userId,goodsId);
			if(goods != null){
				//商品图片信息
				List<GoodsImage> goodsImageList = goodsImageService.selectByGoodsId(Long.parseLong(goods.getGoodsId()));
				if(!goodsImageList.isEmpty() && goodsImageList.size() > 0){
					goods.setGoodsImages(goodsImageList);
				}
				//商品用户信息
				GoodsUser user = userService.selectByUserId(goods.getUserId());
				if(user != null){
					goods.setUser(user);
				}
				//商品颜色
				List<GoodsColor> goodsColors = goodsSizeService.selectGoodsColorByGoodsId(Long.parseLong(goods.getGoodsId()));
				if(!goodsColors.isEmpty()){
					goods.setGoodsColorList(goodsColors);
				}
				//商品尺码信息
				/*List<GoodsSize> goodsSizeList = goodsSizeService.selectByGoodsId(goods.getGoodsId());
				if(!goodsSizeList.isEmpty() && goodsSizeList.size() > 0){
					goods.setGoodsSizeList(goodsSizeList);
				}*/
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return goods;
	}

	@Override
	public HomeMadeExample chooseFabric(long goodsId, long userId) {
		Goods goods= null;
		HomeMadeExample homeMadeExample =new HomeMadeExample();
		try {
			goods = goodsService.selectByGoodsId(goodsId);
			if(goods != null){
				//商品尺码面料信息
				List<GoodsSize> goodsSizeList = goodsSizeService.selectByGoodsId(Long.parseLong(goods.getGoodsId()));
				if(!goodsSizeList.isEmpty() && goodsSizeList.size() > 0){
					homeMadeExample.setGoodsSizeList(goodsSizeList);
				}
				//我的尺码信息
				List<UserSizeInfo> userSizeInfoList = userSizeInfoService.selectByUserIdList(userId);
				if(!userSizeInfoList.isEmpty() && userSizeInfoList.size() > 0){
					homeMadeExample.setUserSizeInfoList(userSizeInfoList);
				}
				//我的地址信息(默认地址)
				List<UserAddress> userAddressList = userAddressService.selectByUserId(userId);
				if(!userAddressList.isEmpty() && userAddressList.size() > 0){
					homeMadeExample.setUserAddress(userAddressList.get(0));
				}
			}
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return homeMadeExample;
	}

	@Override
	public PageUtil<Topic> homeMadeIndexPage(long userId, Integer pageNow) {
		PageUtil<Topic> page =new PageUtil<>();
		try {
			if(pageNow == 0){
				pageNow=1;
			}
			
			page = topicService.indexTopicList(userId, pageNow,AppModuleEnum.DesigerMake);
		} catch (Exception e) {
			log.error(e.getMessage());
		}
		return page;
	}
    
    
}
